<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class ProductDeleteButton
{
    public function __construct()
    {

    }
    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        
        $('.product_delete').click(function () {
           var that = this;
           var id = $(that).attr('data-id');
        
           $.ajax({
                url: '/admin/common/product/ishaschannel',
                type: 'post',
                data: {
                    id: id
                },
                success:function(res){
                    if(res.code == 0){
                        layer.confirm('<\span style="color:red;">此产品正在被使用<\/span>，您确定删除吗?', function(index){
                              $.ajax({
                                url: '/admin/common/product/delete',
                                type: 'post',
                                data: {
                                    id: id
                                },
                                success:function(res){
                                    if(res.code == 0){
                                        layer.msg('删除成功！');
                                        layer.close(index);
                                        location.href = location.href
                                    }else{
                                        layer.msg('删除失败！');
                                    }
                                }
                              });
                        });
                    }else{
                        layer.confirm('您确定删除吗?', function(index){
                              $.ajax({
                                url: '/admin/common/product/delete',
                                type: 'post',
                                data: {
                                    id: id
                                },
                                success:function(res){
                                    if(res.code == 0){
                                        layer.msg('删除成功！');
                                        layer.close(index);
                                        location.href = location.href
                                    }else{
                                        layer.msg('删除失败！');
                                    }
                                }
                              });
                        });   
                    }   
                }
           });
       });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        return '';
    }

    public function __toString()
    {
        return $this->render();
    }
}